package com.unitechs.commons.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 锁工具
 */
public final class ZyucLockUtils {
    private ZyucLockUtils() {
        //防止实例化
        throw new AssertionError();
    }

    private static final ConcurrentHashMap<Object, Lock> locks = new ConcurrentHashMap<>();

    public static void zyucExecuteWithLock(Object seqName, Runnable action) {
        Lock lock = locks.computeIfAbsent(seqName, k -> new ReentrantLock());
        lock.lock();
        try {
            action.run();
        } finally {
            lock.unlock();
        }
    }
}